#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "mockobjects/mockchanmanager.h"
#include <QSignalSpy>

#include "components/chan.h"
#include "components/board.h"
#include "ui/mainwindowcontroller.h"

namespace
{
	using namespace Wishmaster;
	using ::testing::_;
	using ::testing::NiceMock;

	class MainWindowControllerSubclass : public MainWindowController
	{
	public:
		explicit MainWindowControllerSubclass(ChanManager* manager, MainWindow *parent) : MainWindowController(manager, parent) {}
	protected:
		virtual bool shouldBeRefreshed(Board* b)
		{
			return true;
		}
	};


	class TestMainWindowController : public ::testing::Test
	{
	public:
		virtual void SetUp()
		{
			controller = new MainWindowControllerSubclass(&chanmanager, 0);
			makeChanHierarchy();
		}

		virtual void TearDown()
		{
			delete controller;
		}

		void makeChanHierarchy()
		{
			Chan* chan = new Chan("test", 0, &chanmanager);
			Board* b = new Board("board", chan);
			chan->addBoard(b);
			chanmanager.chanlist()->addChan(chan);
		}

		NiceMock<MockObjects::MockChanManager> chanmanager;
		MainWindowControllerSubclass* controller;
	};

	TEST_F(TestMainWindowController, setActiveChanIndex_ifIndexCorrect_emits_boardListChanged)
	{
		QSignalSpy spy(controller, SIGNAL(boardListChanged(QStringList)));

		controller->setActiveChanIndex(0);

		ASSERT_EQ(1, spy.count());
	}

	TEST_F(TestMainWindowController, setActiveChanIndex_ifIndexIncorrect_doesntemit_boardListChanged)
	{
		QSignalSpy spy(controller, SIGNAL(boardListChanged(QStringList)));

		controller->setActiveChanIndex(12);

		ASSERT_EQ(0, spy.count());
	}

	TEST_F(TestMainWindowController, setActiveBoardIndex_refreshesBoard)
	{
		EXPECT_CALL(chanmanager, requestBoardIndex(_, _, _));

		controller->setActiveChanIndex(0);
		controller->setActiveBoardIndex(0);
	}

	TEST_F(TestMainWindowController, setActiveBoardIndex_emits_threadListChanged)
	{
		QSignalSpy spy(controller, SIGNAL(threadListChanged()));

		controller->setActiveChanIndex(0);
		controller->setActiveBoardIndex(0);

		ASSERT_EQ(1, spy.count());
	}
}
